package com.jy.adscm.zltx.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.zltx.bean.QsFile;
import com.jy.adscm.zltx.bean.QsIssueRecyclingQsfile;
import com.jy.adscm.zltx.mapper.mysql.QsFileMapper;
import com.jy.adscm.zltx.mapper.mysql.QsIssueRecyclingQsfileMapper;
import com.jy.adscm.zltx.service.QsIssueRecyclingQsfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 文件发放回收记录 服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-10-23
 */
@Service
public class QsIssueRecyclingQsfileServiceImpl extends ServiceImpl<QsIssueRecyclingQsfileMapper, QsIssueRecyclingQsfile> implements QsIssueRecyclingQsfileService {
   @Autowired
   QsIssueRecyclingQsfileMapper qsIssueRecyclingQsfileMapper;
   @Autowired
    QsFileMapper qsFileMapper;
   @Autowired
    JyUserMapper jyUserMapper;
    @Override
    public List<QsIssueRecyclingQsfile> selectByFileId(Integer fileId) {
        Wrapper<QsIssueRecyclingQsfile> wrapper =new EntityWrapper<>();
        wrapper.eq("fileId",fileId);
        List<QsIssueRecyclingQsfile> qsIssueRecyclingQsfiles = qsIssueRecyclingQsfileMapper.selectList(wrapper);
        for (QsIssueRecyclingQsfile qsIssueRecyclingQsfile : qsIssueRecyclingQsfiles) {
            if (qsIssueRecyclingQsfile.getIssuer()!=null&&!"".equals(qsIssueRecyclingQsfile.getIssuer())){
                JyUser jyUser = jyUserMapper.selectById(qsIssueRecyclingQsfile.getIssuer());
                qsIssueRecyclingQsfile.setIssuerName(jyUser.getName());
            }
            if (qsIssueRecyclingQsfile.getAccepter()!=null&&!"".equals(qsIssueRecyclingQsfile.getAccepter())){
                JyUser jyUser = jyUserMapper.selectById(qsIssueRecyclingQsfile.getAccepter());
                qsIssueRecyclingQsfile.setAccepterName(jyUser.getName());
            }
            if (qsIssueRecyclingQsfile.getRecyclingPerson()!=null&&!"".equals(qsIssueRecyclingQsfile.getRecyclingPerson())){
                JyUser jyUser = jyUserMapper.selectById(qsIssueRecyclingQsfile.getRecyclingPerson());
                qsIssueRecyclingQsfile.setRecyclingPersonName(jyUser.getName());
            }
        }
        return qsIssueRecyclingQsfiles;
    }

    @Override
    public boolean insertQsIssueRecyclingQsfile(QsIssueRecyclingQsfile qsIssueRecyclingQsfile) throws AdscmSystemException {
        boolean insert=false;
        if (qsIssueRecyclingQsfile.getFileId()!=null&&!"".equals(qsIssueRecyclingQsfile.getFileId())){
           QsFile qsFile = qsFileMapper.selectById(qsIssueRecyclingQsfile.getFileId());
           qsIssueRecyclingQsfile.setIssueCode(qsFile.getFileCode());
           qsIssueRecyclingQsfile.setVersionCode(qsFile.getVersionCode());
           qsIssueRecyclingQsfile.setAcceptTime(new Date());
            insert = super.insert(qsIssueRecyclingQsfile);
       }
        
        return insert;
    }
}
